<?php

/**
 * nota actions.
 *
 * @package    cronos-doctrine
 * @subpackage nota
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class notaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      if ( ($request->isXmlHttpRequest()) && ($request->getParameter('idUser'))){
        $nota = new Nota();
        $this->notas = $nota->getMensaje($request->getParameter('idUser'), $request->getParameter('status'));
        $this->idUser =  $request->getParameter('idUser');
        $this->parent =  $request->getParameter('parent');
    }
  }

  public function executeNew(sfWebRequest $request)
  {
       //Recibe la variable IdUser para poder asociarlo al usuario
     if ( ($request->isXmlHttpRequest()) && ($request->getParameter('idUser'))){
        //Busqueda del usuario por el Id
         $usuario = Doctrine_Core::getTable('Usuario')->find(array($request->getParameter('idUser')));
         //Armado del Objeto Mensaje Alerta
         $nota = new Nota();
         $nota->setNotaStatusId($request->getParameter('status'));
         $nota->setUsuario($usuario);

         //Arma Formulario Mensaje Alerta para el usuario en cuestion
         $this->form = new NotaForm($nota);
         $this->form->setWidget('usuario_id', new sfWidgetFormInputHidden(array(),array('value' => $request->getParameter('idUser'))));
         $this->form->setWidget('nota_status_id', new sfWidgetFormInputHidden(array(),array('value' => $request->getParameter('status'))));
          //Reasigna la variable usuario para su actualizacion luego Jquery
         $this->idUser = $request->getParameter('idUser');
         $this->parent =  $request->getParameter('parent');
     }

    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    //Captura Variable Id Usuario
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');
    
    $this->form = new NotaForm();

    $this->processForm($request, $this->form, $this->idUser,$this->parent);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($nota = Doctrine_Core::getTable('Nota')->find(array($request->getParameter('id'))), sprintf('Object nota does not exist (%s).', $request->getParameter('id')));
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');
    $this->form = new NotaForm($nota);
    $this->form->setWidget('usuario_id', new sfWidgetFormInputHidden(array(),array('value' => $request->getParameter('idUser'))));
    $this->form->setWidget('mensaje_status_id', new sfWidgetFormInputHidden(array(),array('value' => $request->getParameter('status'))));
         
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($nota = Doctrine_Core::getTable('Nota')->find(array($request->getParameter('id'))), sprintf('Object nota does not exist (%s).', $request->getParameter('id')));
    //Captura Variable Id Usuario
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');

    $this->form = new NotaForm($nota);
    $this->processForm($request, $this->form, $this->idUser,$this->parent);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($nota = Doctrine_Core::getTable('Nota')->find(array($request->getParameter('id'))), sprintf('Object nota does not exist (%s).', $request->getParameter('id')));
    $nota->delete();

    $this->redirect('nota/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form,$idUser,$parent)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $nota = $form->save();

      $this->redirect('nota/edit?id='.$nota->getId()."&idUser=".$idUser."&parent=".$parent);
    }
  }
}
